<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: huajie <banhuajie@163.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;

/**
 * 后台内容控制器
 * @author huajie <banhuajie@163.com>
 */
class AdController extends AdminController {
 
   protected function _initialize(){
     parent::_initialize();
   }

   public function index(){
    $title=I('title');
	 if(!empty($title)){
	   $map['title']=['like',"%$title%"];
	 }
     $sortfld=I('sortfld',"ad_id",'trim');
     $sortord=I('sortord',"desc",'trim');  	 
     $list=$this->lists('ad',$map,"`$sortfld` $sortord");
	 $this->assign('list',$list);
     $posList=M('ad_position')->where(['is_open'=>1])->getField('position_id,position_name',true);
     $this->assign('plist',$posList);	
     $this->display();
   }


public function adAdm(){
     if(IS_POST){
            $data = I('post.');		
			$data['start_time']=strtotime($data['start_time']);
			$data['end_time']=strtotime($data['end_time']);

			if(empty($data['id'])){
                $ret = M('ad')->add($data);
			}else{			 
			   $ret = M('ad')->where(['ad_id'=>$data['id']])->save($data);
			}
             if($ret!==false){
        	   $this->ajaxReturn(1,"操作成功");
        }else{
        	   $this->ajaxReturn(0,"操作失败");
        }

     }else{
          $id=I('id');
		  $info=M('ad')->where(['ad_id'=>$id])->find();
		  $this->assign('id',$id);
		  $posList=M('ad_position')->where(['is_open'=>1])->select();
		  $this->assign('plist',$posList);	

		  $this->assign('info',$info);		 
     	  $this->ajaxReturn(1,'success',$this->fetch());
     }

 }

    /**
     * 删除   
     */
    public function adDel(){
        $id = array_unique((array)I('id',0));

        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }

        $map = array('ad_id' => array('in', $id) );
        if(M('ad')->where($map)->delete()){
            session('ADMIN_MENU_LIST',null);
            //记录行为
            action_log('delete ad', 'ad', $id, UID);
            $this->success('删除成功');
        } else {
            $this->error('删除失败！');
        }
    }

   public function posList(){
	 $title=I('title');
	 if(!empty($title)){
	   $map['title']=['like',"%$title%"];
	 }
     $sortfld=I('sortfld',"position_id",'trim');
     $sortord=I('sortord',"desc",'trim');  	 
     $list=$this->lists('ad_position',$map,"`$sortfld` $sortord");
	 $this->assign('list',$list);
     $this->display();
   }



 public function posAdm(){
     if(IS_POST){
            $data = I('post.');		 
			if(empty($data['id'])){
                $ret = M('ad_position')->add($data);
			}else{			 
			   $ret = M('ad_position')->where(['position_id'=>$data['id']])->save($data);
			}
             if($ret!==false){
        	   $this->ajaxReturn(1,"操作成功");
        }else{
        	   $this->ajaxReturn(0,"操作失败");
        }

     }else{
          $id=I('id');
		  $info=M('ad_position')->where(['position_id'=>$id])->find();
		  $this->assign('id',$id);
		  $this->assign('info',$info);
		  SL($info);
     	  $this->ajaxReturn(1,'success',$this->fetch());
     }


 }

    /**
     * 删除   
     */
    public function posDel(){
        $id = array_unique((array)I('id',0));

        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }

        $map = array('position_id' => array('in', $id) );
        if(M('ad_position')->where($map)->delete()){
            session('ADMIN_MENU_LIST',null);
            //记录行为
            action_log('delete ad_pos', 'ad_pos', $id, UID);
            $this->success('删除成功');
        } else {
            $this->error('删除失败！');
        }
    }

 
}